package legoserver;

import java.io.*;
import java.net.*;
import java.awt.Point;
import javax.swing.ImageIcon;

public class TestServer
{

	/**
	 * @author Eric Enger
	 * @param args
	 */
	
	private final static int port = 9002;
	private Socket socket;
	private ServerSocket serverSocket;
	private Point point;
	private ImageIcon pic;
        private ObjectOutputStream output;
        private String echo = "Echo";
        private String command;
	
	public TestServer()
	{
            
        try {
			serverSocket = new ServerSocket(port);
			socket = serverSocket.accept();
                            try {
                                    output = new ObjectOutputStream(socket.getOutputStream());
                                }
                                    catch (IOException e) {
                                                            System.out.println(e);
                                };
			System.out.println("SERVER: socket connected successfully");
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			while(true)
			{
				//command = (String) in.readObject();
                                String command = (String) in.readObject();
                                //System.out.println("SERVER: COMMAND=" + command);
                                if(command.equals("FWD")) { System.out.println("FWD"); };
                                if(command.equals("STP")) { System.out.println("STP"); };
                                if(command.equals("BCK")) { System.out.println("BCK"); };
                                if(command.equals("CW")) { System.out.println("CW"); };
                                if(command.equals("CCW")) { System.out.println("CCW"); };
                                if(command.equals("CONNECT")) { 
                                    System.out.println("CONNECT");
                                    System.out.println(">OK");
                                    output.writeObject("OK");
                                };
                                
                                
                                
				//point = (Point) in.readObject();
				//System.out.println("SERVER: x: " + point.getX() + "\ty:" + point.getY());
				//pic = (ImageIcon) in.readObject();
				//TestContainer tc = new TestContainer(point, pic);
                                //echo to client
                                output.writeObject("OK");
			}
		}
		catch(StreamCorruptedException sc)
		{
			sc.printStackTrace();
			System.out.println("SERVER: ObjectInputStream corrupted");
		}
		catch(BindException b)
		{
			b.printStackTrace();
			System.out.println("SERVER: Bind Exception");
			System.out.println("SERVER: Address in use");
		}
		catch(IOException io)
		{
			io.printStackTrace();
			System.out.println("SERVER: I/O Exception");
		}
		//required to cast to type Point
		catch(ClassNotFoundException cnf)
		{
			cnf.printStackTrace();
			System.out.println("SERVER: Problem casting");
		}
		
		finally
		{
			try
			{
				socket.close();
			}
			catch(IOException io)
			{
				io.printStackTrace();
				System.out.println("error closing socket in TesetServer");
			}
		}
		
	}
	
	

}